﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBienLaiThuDao : IBienLaiThuDao
    {
        public SqlServerBienLaiThuDao()
        {
            SQLUtility.TableName = "BienLaiThu";
        }

        //Trả về đối tượng báo ăn
        private BienLaiThu MakeBienLaiThu(DataRow row)
        {
            long _id = long.Parse(row["ID"].ToString());
            Guid _studentID = Guid.Parse(row["StudentID"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            string _so = row["So"].ToString();
            DateTime _ngay = DateTime.Parse(row["Ngay"].ToString());
            int _thuThang = int.Parse(row["ThuThang"].ToString());
            decimal _tongThu = decimal.Parse(row["TongThu"].ToString());
            int _soveTrongthang = int.Parse(row["SoveTrongthang"].ToString());
            int _sovePhaimua = int.Parse(row["SovePhaimua"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());
            return new BienLaiThu(_id, _studentID, _schoolYearID, _classID, _so, _ngay, _thuThang, _tongThu, _soveTrongthang, _sovePhaimua, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<BienLaiThu> MakeMakeBienLaiThus(DataTable dt) 
        {
            IList<BienLaiThu> list = new List<BienLaiThu>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBienLaiThu(row));
            }
            return list;
        }

        public IList<Business_Objects.BienLaiThu> GetBienLaiThu()
        {
            SQLUtility.TableName = "BienLaiThu";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeMakeBienLaiThus(dt);
        }

        public Business_Objects.BienLaiThu GetBienLaiThuByID(string BienLaiThuID)
        {
            SQLUtility.TableName = "BienLaiThu";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@ID", BienLaiThuID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBienLaiThu(row);
            }
        }

        public void InsertBienLaiThu(Business_Objects.BienLaiThu BienLaiThu)
        {
            SQLUtility.TableName = "BienLaiThu";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(BienLaiThu), true);
        }

        public int UpdateBienLaiThu(Business_Objects.BienLaiThu BienLaiThu)
        {
            SQLUtility.TableName = "BienLaiThu";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(BienLaiThu)); }
            catch { return -1; }
        }

        public int DeleteBienLaiThu(Business_Objects.BienLaiThu BienLaiThu)
        {
            SQLUtility.TableName = "BienLaiThu";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(BienLaiThu)); }
            catch { return -1; }
        }

        private DbParameter[] Take(BienLaiThu BienLaiThu)
        {
            DbParameter[] parameters = new DbParameter[11];
            parameters[0] = new SqlParameter("@ID", BienLaiThu.Id);
            parameters[1] = new SqlParameter("@StudentID", BienLaiThu.StudentID);
            parameters[2] = new SqlParameter("@SchoolYearID", BienLaiThu.SchoolYearID);
            parameters[3] = new SqlParameter("@ClassID", BienLaiThu.ClassID);
            parameters[4] = new SqlParameter("@So", BienLaiThu.So);
            parameters[5] = new SqlParameter("@Ngay", BienLaiThu.Ngay);

            parameters[6] = new SqlParameter("@ThuThang", BienLaiThu.ThuThang);
            parameters[7] = new SqlParameter("@TongThu", BienLaiThu.TongThu);
            parameters[8] = new SqlParameter("@SoveTrongthang", BienLaiThu.SoveTrongthang);
            parameters[9] = new SqlParameter("@SovePhaimua", BienLaiThu.SovePhaimua);
            parameters[10] = new SqlParameter("@SchoolID", BienLaiThu.SchoolID);

            return parameters;
        }


        public List<BienLaiThu> GetBienLaiThuByClassIdAndSchoolSchoolYear(string schoolYearID, int schoolId, string classId)
        {
            DbParameter[] parameters = new DbParameter[3];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@SchoolYearID", schoolYearID);
            parameters[2] = new SqlParameter("@ClassID", classId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBienLaiThusByAndSchoolIDAndSchoolYearIDInClass", parameters);
            return MakeMakeBienLaiThus(dt).ToList();
        }


        public int InsertGetId(BienLaiThu obj)
        {
            SQLUtility.TableName = "BienLaiThu";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(obj), true);
            return newID;
        }
    }
}
